package com.xuecheng.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuecheng.api.content.teacher.CourseTeacherDTO;
import com.xuecheng.api.content.teacher.CourseTeacherVO;
import com.xuecheng.common.domain.code.CommonErrorCode;
import com.xuecheng.common.enums.common.CommonEnum;
import com.xuecheng.common.enums.content.CourseAuditEnum;
import com.xuecheng.common.enums.content.CourseModeEnum;
import com.xuecheng.common.exception.ExceptionCast;
import com.xuecheng.common.util.StringUtil;
import com.xuecheng.content.common.constant.ContentErrorCode;
import com.xuecheng.content.convert.TeacherCourse;
import com.xuecheng.content.convert.TeachplanConvert;
import com.xuecheng.content.entity.CourseBase;
import com.xuecheng.content.entity.CourseTeacher;
import com.xuecheng.content.mapper.CourseTeacherMapper;
import com.xuecheng.content.service.CourseBaseService;
import com.xuecheng.content.service.CourseTeacherService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 课程-教师关系表 服务实现类
 * </p>
 *
 * @author xuan
 */
@Slf4j
@Service
public class CourseTeacherServiceImpl extends ServiceImpl<CourseTeacherMapper, CourseTeacher> implements CourseTeacherService {

    @Autowired
    private CourseBaseService courseBaseService;

    /**
     * 教师信息的查询
     * 业务分析
     * 1.判断关键数据
     * 2.判断业务数据
     * 3.根据id查询
     * 4.返回
     *
     * @param courseBaseId id
     * @return CourseTeacherDTO
     */
    public List<CourseTeacherDTO> selectAll(Long courseBaseId, Long companyId) {

        if (ObjectUtils.isEmpty(courseBaseId)
                || ObjectUtils.isEmpty(companyId)
        ) {
            ExceptionCast.cast(CommonErrorCode.E_100101);
        }

        LambdaQueryWrapper<CourseBase> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CourseBase::getStatus, CommonEnum.USING_FLAG.getCodeInt())
                .eq(CourseBase::getCompanyId, companyId)
                .eq(CourseBase::getId,courseBaseId);

        CourseBase one = courseBaseService.getOne(wrapper);
        if (ObjectUtils.isEmpty(one)) {
            ExceptionCast.cast(ContentErrorCode.E_120013);
        }

        //判断老师信息
        LambdaQueryWrapper<CourseTeacher> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CourseTeacher::getCourseId, courseBaseId);

        List<CourseTeacher> list = this.list(queryWrapper);

        if (ObjectUtils.isEmpty(list)) {
            ExceptionCast.cast(CommonErrorCode.E_100104);
        }

        List<CourseTeacherDTO> courseTeacherDTOS = TeachplanConvert.INSTANCE.entity2dtos(list);

        return courseTeacherDTOS;


    }

    /**
     * 添加或者修改教师信息
     * 业务分析
     * 1.判断关键数据
     * courseid photograph position teacherName
     * 2.判断业务数据
     * 判断是增加还是修改
     * 有教师id就是修改 没有就添加
     *
     * @param dto       基本信息
     * @param companyId 公司id
     * @return
     */
    @Transactional
    public CourseTeacherDTO createOrModifyCourseTeacher(CourseTeacherDTO dto, Long companyId) {
        //判断关键数据
        if (ObjectUtils.isEmpty(dto.getCourseId())
               // || StringUtil.isBlank(dto.getPhotograph())
                || StringUtil.isBlank(dto.getPosition())
                || StringUtil.isBlank(dto.getTeacherName())
                || ObjectUtils.isEmpty(companyId)
        ) {
            ExceptionCast.cast(CommonErrorCode.E_100101);
        }
        //判断课程信息是否存在
        this.verifyCourseBase(dto.getCourseId(), companyId);
        //判断是添加还是修改
        CourseTeacherDTO courseTeacherDTO;
        if (ObjectUtils.isEmpty(dto.getCourseTeacherId())) {
            //添加
            courseTeacherDTO = this.creatrCourseBaseTeacher(dto, companyId);
        } else {
            courseTeacherDTO = this.modifyCourseTeacher(dto, companyId);
        }
        return courseTeacherDTO;
    }


    /**
     * 删除教师信息
     * 业务分析
     * 判断关键数据
     * 判断业务数据
     *
     * @param courseBase      基本信息id
     * @param courseTeacherId 教师信息id
     * @param companyId       公司id
     */
    @Transactional
    public void removeCourseTeacher(Long courseBase, Long courseTeacherId, Long companyId) {
        //判断关键数据
        if (ObjectUtils.isEmpty(courseBase)
                || ObjectUtils.isEmpty(courseTeacherId)
                || ObjectUtils.isEmpty(companyId)
        ) {
            ExceptionCast.cast(CommonErrorCode.E_100101);
        }

        //判断业务数据
        verifyCourseBase(courseTeacherId,companyId);

        LambdaQueryWrapper<CourseTeacher> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CourseTeacher::getId,courseTeacherId)
                .eq(CourseTeacher::getCourseId,courseBase);
        boolean remove = this.remove(wrapper);
        //boolean removeById = this.removeById(courseTeacherId);

        if (!remove) {
            ExceptionCast.cast(ContentErrorCode.E_120033);
        }

    }

    /**
     * 修改教师信息
     *
     * @param dto       基本信息
     * @param companyId id
     * @return
     */
    private CourseTeacherDTO modifyCourseTeacher(CourseTeacherDTO dto, Long companyId) {
        LambdaUpdateWrapper<CourseTeacher> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(CourseTeacher::getCourseId, dto.getCourseId())
                .set(CourseTeacher::getCoursePubId, dto.getCoursePubId())
                .set(CourseTeacher::getTeacherName, dto.getTeacherName())
                .set(CourseTeacher::getPhotograph, dto.getPhotograph())
                //.set(CourseTeacher::getCreateDate, LocalDateTime.now())
                .set(CourseTeacher::getIntroduction, dto.getIntroduction())
                .eq(CourseTeacher::getId, dto.getCourseTeacherId());

        boolean update = this.update(updateWrapper);
        if (!update) {
            ExceptionCast.cast(ContentErrorCode.E_120032);
        }

        CourseTeacher byId = this.getById(dto.getCourseTeacherId());
        CourseTeacherDTO courseTeacherDTO = TeacherCourse.INSTANCE.entityTwo2Dto(byId);

        return courseTeacherDTO;
    }

    /**
     * 添加数据
     *
     * @param dto       基本信息
     * @param companyId id
     * @return
     */
    private CourseTeacherDTO creatrCourseBaseTeacher(CourseTeacherDTO dto, Long companyId) {
        CourseTeacher courseTeacher = TeachplanConvert.INSTANCE.DtoTwo2Dto(dto);

        boolean save = this.save(courseTeacher);
        if (!save) {
            ExceptionCast.cast(ContentErrorCode.E_120031);
        }

        CourseTeacher byId = this.getById(courseTeacher.getId());

        CourseTeacherDTO courseTeacherDTO = TeacherCourse.INSTANCE.entityTwo2Dto(byId);

        return courseTeacherDTO;
    }

    //判断课程是否存在
    private void verifyCourseBase(Long courseBaseId, Long companyId) {
        LambdaQueryWrapper<CourseBase> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CourseBase::getCompanyId, companyId)
                .eq(CourseBase::getId, courseBaseId)
                .eq(CourseBase::getStatus, CommonEnum.USING_FLAG.getCodeInt());
        CourseBase one = courseBaseService.getOne(wrapper);
        if (ObjectUtils.isEmpty(one)) {
            ExceptionCast.cast(ContentErrorCode.E_120013);
        }


    }


}
